package Project;

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.HashSet;

public class SnakesCRUD extends JFrame {
  private HashSet hs = new HashSet<String>();
  private Statement query;
  private Connection c;
  
  public SnakesCRUD() {
    super("Snakes - Create, Update, Delete");
    initComponents();
    this.setLocationRelativeTo(null);
    
    startConnection();
  }
  
  public final void startConnection() {
    try {
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // aqui virá o driver do banco
      
      this.c = DriverManager.getConnection("jdbc:sqlserver://GODOT\\SQLExpress;databaseName=master;", "sa", "root1234");
      this.query = this.c.createStatement();
    }
    catch(Exception e) {
      JOptionPane.showMessageDialog(null, "Error connecting to the database!", "Snakes", JOptionPane.ERROR_MESSAGE);
      System.exit(0);
    }
  }

  public void addCard(JPanel panel, String name) {
    this.principal.add(panel, name);
  }
  
  public void updateLayout(String card) {
    ((CardLayout)this.principal.getLayout()).show(this.principal, card);
    this.principal.validate();
  }
  
  public void executeCommand(String sql) throws SQLException {
    if (this.query.executeUpdate(sql) == 0)
      throw new SQLException("Query not completed successfully.");
  }
  
  public ResultSet executeQuery(String sql) throws SQLException {
    return this.query.executeQuery(sql);
  }
  
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        principal = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        principal.setLayout(new java.awt.CardLayout());

        jMenu1.setText("Colaborators");

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Member");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Animals");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Others");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("States");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Colors");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(principal, javax.swing.GroupLayout.DEFAULT_SIZE, 619, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(principal, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
    if (!hs.contains(NewState.NAME)) {
      NewState ns = new NewState(this);
      this.addCard(ns, NewState.NAME);
      this.hs.add(NewState.NAME);
    }
    
    this.updateLayout(NewState.NAME);
  }//GEN-LAST:event_jMenuItem1ActionPerformed

  private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
    if (!hs.contains(NewColor.NAME)) {
      NewColor nc = new NewColor(this);
      this.addCard(nc, NewColor.NAME);
      this.hs.add(NewColor.NAME);
    }
    
    this.updateLayout(NewColor.NAME);
  }//GEN-LAST:event_jMenuItem2ActionPerformed

  private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
    if (!hs.contains(NewMember.NAME)) {
      NewMember nm = new NewMember(this);
      this.addCard(nm, NewMember.NAME);
      this.hs.add(NewMember.NAME);
    }
    
    this.updateLayout(NewMember.NAME);
  }//GEN-LAST:event_jMenuItem3ActionPerformed

  public static void main(String args[]) {
    try {
      javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception ex) {
      JOptionPane.showMessageDialog(null, ex.getMessage());
    }
    
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new SnakesCRUD().setVisible(true);
      }
    });
  }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel principal;
    // End of variables declaration//GEN-END:variables
}
